package com.lhy.mymall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.lhy.mymall.common.Constant;
import com.lhy.mymall.exception.ExceptionEnum;
import com.lhy.mymall.exception.MallException;
import com.lhy.mymall.mapper.UserMapper;
import com.lhy.mymall.pojo.User;
import com.lhy.mymall.service.UserService;
import com.lhy.mymall.utils.MD5Util;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;

/**
 * @author: 卢辉宇
 * @date: 2022/12/2 21:22
 * @description:
 */

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public User getUser(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void register(String username, String password,String emailAddress) throws MallException {
        //查询用户名是否已存在
        User result = userMapper.selectByName(username);
        //用户名存在
        if (result != null) {
            //因为这个是service层，不能直接像controller层一样直接返回给前端，所以直接把异常向上抛出
            throw new MallException(ExceptionEnum.USERNAME_EXISTED);
        }

        //正式注册
        User user = new User();
        user.setUsername(username);
        user.setEmailAddress(emailAddress);
        /*user.setPassword(password);*/
        try {
            user.setPassword(MD5Util.getMD5Str(password));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        //插入到数据库中
        int count = userMapper.insertRegister(user);

        //判断是否插入成功
        if (count == 0) {
            throw new MallException(ExceptionEnum.INSERT_FAILED);
        }
    }

    @Override
    public User login(String username, String password, String verificationCode) throws MallException {
        /*把password转为md5的，让md5的来和库里的密码对比*/
        String md5Password = null;
        try {
            md5Password = MD5Util.getMD5Str(password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        /*根据用户名查找数据库对应的md5密码是否一致*/
        User user = userMapper.selectLogin(username, md5Password);
        if (ObjectUtils.isEmpty(user)) {
            throw new MallException(ExceptionEnum.WRONG_PASSWORD);
        }
        //从redis中获取验证码
        String code = stringRedisTemplate.opsForValue().get(Constant.LOGIN_CODE_PRE + user.getEmailAddress());
        if(StrUtil.isBlank(code)||!verificationCode.equals(code)){
            throw new MallException(ExceptionEnum.WRONG_VERIFICATION_CODE);
        }
        return user;
    }

    @Override
    public void updateSignature(User user) throws MallException {
        //通过mapper层插入数据库
        int UpdateCount = userMapper.updateByPrimaryKey(user);
        //是否更新成功
        if (UpdateCount > 1 || UpdateCount == 0) {
            throw new MallException(ExceptionEnum.UPDATE_FAILED);
        }
    }

    @Override
    public boolean checkAdminRole(User user) {
        /*1是普通用户，2是管理员*/
        return user.getRole().equals(2);
    }

    @Override
    public boolean checkEmailRegistered(String emailAddress) {
        //查找是否数据库中已经存在email
        User user = userMapper.selectOneByEmailAddress(emailAddress);
        if (user != null) {
            /*已注册*/
            return true;
        }
        return false;
    }
}
